<template>
  <el-dialog :visible.sync="isShow" :before-close="handleCancelModal" :close-on-click-modal="false" width="500px">
    <span slot="title" v-text="formParams['supplier_id'] ? '编辑货主' : '创建货主'" />
    <!-- 数据表单 -->
    <el-form ref="formParams" :model="formParams" label-width="120px" size="small" style="padding-right: 40px">
      <el-form-item class="is-required" prop="name" label="姓名：">
        <el-input v-model="formParams.name" placeholder="请输入货主姓名" />
      </el-form-item>
      <el-form-item prop="mark" label="助记码：">
        <el-input v-model="formParams.mark" placeholder="请输入助记码" />
      </el-form-item>
      <el-form-item prop="phone" label="手机号：">
        <el-input v-model="formParams.phone" placeholder="请输入手机号" />
      </el-form-item>
      <el-form-item prop="wechat_name" label="微信昵称：">
        <el-input v-model="formParams.wechat_name" placeholder="请输入微信昵称" />
      </el-form-item>
      <el-form-item prop="wechat_code" label="微信号：">
        <el-input v-model="formParams.wechat_code" placeholder="请输入微信号" />
      </el-form-item>
      <el-form-item prop="description" label="备注：">
        <el-input v-model="formParams.description" placeholder="请输入备注" />
      </el-form-item>
      <el-form-item prop="sort" label="排序：">
        <el-input v-model="formParams.sort" placeholder="请输入排序" />
      </el-form-item>
      <el-form-item prop="status" label="货主状态：">
        <el-radio-group v-model="formParams.status" size="mini">
          <el-radio-button :label="0"><span style="padding: 0 5px">停用</span></el-radio-button>
          <el-radio-button :label="1"><span style="padding: 0 5px">正常</span></el-radio-button>
        </el-radio-group>
      </el-form-item>
    </el-form>
    <!-- 提交按钮 -->
    <div slot="footer" class="dialog-footer">
      <el-button size="small" :disabled="isSubmit" @click="handleCancelModal">取 消</el-button>
      <el-button type="primary" size="small" :loading="isSubmit" @click="handleSubmitForm">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { save } from '@/api/supplier'
import { extract } from '@/utils/tools'

export default {
  props: {
    'isShow': { type: Boolean, default: true },
    'isUpdate': { type: Boolean, default: false },
    'itemData': { type: Object, default: null }
  },
  data: function() {
    return {
      isSubmit: false,
      formParams: {
        supplier_id: null,
        name: null,
        mark: null,
        avatar: null,
        phone: null,
        wechat_name: null,
        wechat_code: null,
        description: null,
        sort: 50,
        status: 1
      }
    }
  },
  mounted: function() {
    if (this.itemData) {
      this.itemData.supplier_id = this.itemData.supplier_id ? this.itemData.supplier_id : null
      this.formParams = extract(this.formParams, this.itemData)
    }
  },
  methods: {
    handleSubmitForm() {
      console.log(this.formParams)
      this.isSubmit = true
      save(this.formParams, this.formParams['supplier_id'] || null).then(function() {
        this.$emit('on-cancel-modal', true)
      }.bind(this)).catch(function() {
        this.isSubmit = false
      }.bind(this))
    },
    // 取消弹出框
    handleCancelModal(refresh = false) {
      if (!this.isSubmit) {
        this.$emit('on-cancel-modal', refresh)
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.partition-container {
  display: flex;
  .partition-left {
    flex-grow: 1;
    margin-right: 15px;
  }
  .partition-right {
    width: 200px;
    height: 262px;
    border: 1px solid #DCDFE6;
    .title {
      height: 31px;
      font-size: 14px;
      font-weight: 700;
      line-height: 31px;
      padding-left: 8px;
      background-color: #f2f2f2;
    }
    .rule-main {
      height: 227px;
      margin-top: 1px;
      overflow-y: scroll;
      .el-tree {
        margin-top: 5px;
      }
    }
  }
}
</style>
